package com.sgb.kjwl.view.ui.activity.manage.special

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivitySpecialMemberBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.sgb.kjwl.view.event.special.InviteEvent
import com.sgb.kjwl.view.ui.fragment.manage.OtherInquiryFragment
import com.sgb.kjwl.view.ui.fragment.manage.WebSiteInquiryFragment
import com.sgb.kjwl.view.ui.fragment.manage.special.InviteMemberFragment
import com.sgb.kjwl.viewmodel.manage.CompanySpecialViewModel
import com.swgk.core.base.di.AppComponent
import com.swgk.core.util.PickSelectEntity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import javax.inject.Inject

/**
 * 企业专项 邀请成员
 */
class InviteMemberActivity : AppActivity() {

    private lateinit var  mBinding : ActivitySpecialMemberBinding

    @Inject
    lateinit var mViewModel : CompanySpecialViewModel

    private lateinit var mAdapter : InviteMemberPageAdapter

    companion object{
        fun launch(context: Context){
            val intent  = Intent(context, InviteMemberActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerSelfComponent.builder().appComponent(appComponent)
            .selfModule(SelfModule()).build().inject(this)
    }

    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_special_member)
        mViewModel.titles.observe(this, {
            if (it != null && it.isNotEmpty()){
                initTabLayout(it)
            }
        })
        mBinding.include.titleView.text = "邀请成员"
        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.include.rightView.text = "邀请成员"
        mBinding.include.rightView.setOnClickListener {
             InviteMemberListActivity.launch(this)
        }
    }

    private fun initTabLayout(titles : List<PickSelectEntity<Any>>) {
        mAdapter = InviteMemberPageAdapter(supportFragmentManager, titles)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = titles.size
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        mBinding.viewPage.currentItem = 0
    }

    override fun init() {
        super.init()
        mViewModel.getTitle()
    }



    class InviteMemberPageAdapter(fm : FragmentManager, titles : List<PickSelectEntity<Any>>) : FragmentPagerAdapter(fm) {
        private var mTitles : List<PickSelectEntity<Any>> ?= null
        init {
            this.mTitles = titles
        }
        override fun getCount(): Int {
            if (mTitles != null){
                return mTitles!!.size
            }
            return 0
        }

        override fun getItem(position: Int): Fragment {
            if (mTitles == null) return OtherInquiryFragment.newInstance()
            return InviteMemberFragment.newInstance(mTitles!![position].no)
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null){
                return mTitles!![position].projectscale
            }
            return ""
        }


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetMessage(event : InviteEvent){
        val fragmentOne = supportFragmentManager.findFragmentByTag(
            "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 0)
        if (fragmentOne != null && fragmentOne is InviteMemberFragment) {
            fragmentOne.refresh()
        }
        val fragmentTwo = supportFragmentManager.findFragmentByTag(
            "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 1)
        if (fragmentTwo != null && fragmentTwo is InviteMemberFragment) {
            fragmentTwo.refresh()
        }
        val fragmentThree = supportFragmentManager.findFragmentByTag(
            "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 2)
        if (fragmentThree != null && fragmentThree is InviteMemberFragment) {
            fragmentThree.refresh()
        }
        val fragmentFour = supportFragmentManager.findFragmentByTag(
            "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 3)
        if (fragmentFour != null && fragmentFour is InviteMemberFragment) {
            fragmentFour.refresh()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}